define(['template', 'jquery','bootstrap','cook','utils'], (template) => {
    class Header {
      constructor () {
         this.loadHTML().then(()=>{
          //只要涉及到header的DOM操作就在这里写  异步完成之后
           this.hrefClick()
           this.getTabs()
           this.calacCartCount()
           this.Logout()
           this.Login()
         })
        }
        loadHTML(){
          return new Promise((resolve,reject)=>{

            $('header').load('/html/modules/header.html',()=>{
              $('.drop-div').hide()
              resolve()
            })
          })
        }
        hrefClick(){
          
          $('#login').on('click',function(){
            window.location.href="/html/login1.html";
          })
          $('#register').on('click',function(){
            window.location.href="/html/register2.html";
          })
        }
        getTabs(){
          $.get('https://xiongmaoyouxuan.com/api/tabs',resp=>{
            if(resp.code ===200){
              let {list} =resp.data
              list =list.slice(1,5)
              
              $('#tabList').append(template('tabListTemplate', { list }))
            }
          })
        }
        calacCartCount(){
          
          let count = 0
          let cart = localStorage.getItem('cart')
          if(cart){
            cart  =JSON.parse(cart)
            count = cart.reduce((num ,shop)=>{
              num+=shop.count
              return num
            },0)
          }
          $('#shopnum').html(count)
          $('#cart span').html(count)
        } 
        //登出
        Logout(){
          
          $('#logout').on('click',()=>{
            
            utils.setCookie('name', '',{expires:-1, path:'/'})
            $('.login').show()
            $('.drop-div').hide()
            
        })
        }
        //登入
        Login(){
          var userName = utils.getCookie('name')
       
          if(userName){
            $('.login').hide()
            $('.drop-div').show()
            $('#username').html(userName)
        }
        }
      }
      
    return new Header()
  })
  